$(document).ready(function () {
    $("#confirm").click(function () {
        var name = $("#username").val();
        var email = $("#email").val();
        var password = $("#password").val();
        var confirmPassword = $("#confirmPassword").val();
        var myReg = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
        if ((name == null || name == "") && (password == null || password == "") && (
            email == null || email == "")) {
            alert("请输入需要修改的信息！");
        } else {
            if (email != null && email != "" && !myReg.test(email)) {
                alert("邮箱格式不对!");
                alert(email)
            } else {
                if (password != confirmPassword) {
                    alert("密码不一致！");
                } else {
                    $.ajax({
                        type: "POST",
                        url: "/changeInformation/getAllUsers",
                        dataType: "json",
                        async: false,
                        success: function (data) {
                            var flag = 0;
                            for (var i in data) {
                                if (name == data[i].name) {
                                    alert("用户名已存在，请重新输入！")
                                    flag = 1;
                                    break;
                                } else {
                                    if (email == data[i].email) {
                                        alert("邮箱被占用，请重新输入！")
                                        flag = 1;
                                        break;
                                    }
                                }
                            }
                            if (flag === 0) {
                                $.ajax({
                                    type: "get",
                                    url: "/session/getSession",
                                    dataType: "json",
                                    success: function (data) {
                                        if (name == null || name == "") {
                                            name = data.user.name;
                                        }
                                        if (email == null || email == "") {
                                            email = data.user.email;
                                        }
                                        if (password == null || password == "") {
                                            password = data.user.password1;
                                        }
                                        $.ajax({
                                            type: "get",
                                            url: "/changeInformation/encrypt",
                                            data: {
                                                name: name,
                                                password: password
                                            },
                                            dataType: "text",
                                            async: false,
                                            success: function (res) {
                                                password = res;
                                            }
                                        })

                                        $.ajax({
                                            type: "get",
                                            url: urlUserCenter + "/user/updateUser",
                                            dataType: "json",
                                            data: {
                                                id: data.user.id,
                                                name: name,
                                                password: password,
                                                email: email
                                            },
                                            success: function () {
                                                alert("修改成功！")
                                                window.location.href = "login.html";
                                            },
                                            error: function () {
                                                alert("修改失败！")
                                            }
                                        })
                                    },
                                    error: function (data) {
                                    }
                                })
                            }
                        },
                        error: function () {
                        }
                    })
                }

            }
        }

    })

});

